package com.wisevar.thirdplatform.gateway.model.common;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;
    private T data;
    private boolean success;

    private ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = ResultCode.SUCCESS.code == code;
    }

    public static <T> ApiResult<T> ok() {
        return ok(null);
    }

    public static <T> ApiResult<T> ok(T data) {
        return build(ResultCode.SUCCESS, ResultCode.SUCCESS.msg, data);
    }

    public static <T> ApiResult<T> paramError() {
        return build(ResultCode.PARAM_ERROR, ResultCode.PARAM_ERROR.msg, null);
    }

    public static <T> ApiResult<T> paramError(String msg) {
        return build(ResultCode.PARAM_ERROR, msg, null);
    }

    public static <T> ApiResult<T> serverError() {
        return build(ResultCode.SERVER_ERROR, ResultCode.SERVER_ERROR.msg, null);
    }

    public static <T> ApiResult<T> serverError(String msg) {
        return build(ResultCode.SERVER_ERROR, msg, null);
    }

    public static <T> ApiResult<T> build(IResultCode code) {
        return build(code, code.getMsg(), null);
    }

    public static <T> ApiResult<T> build(int code, String msg) {
        return build(code, msg, null);
    }

    public static <T> ApiResult<T> build(int code, String msg, T data) {
        return new ApiResult<>(code, msg, data);
    }


    public static <T> ApiResult<T> build(IResultCode code, String msg, T data) {
        return build(code.getCode(), msg, data);
    }

    public static <T> ApiResult<PageResult<T>> ofPage(List<T> content, long totalCount) {
        return ok(PageResult.build(content, totalCount));
    }

}
